package com.gewu.pm.ui.activity.work;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.content.res.AppCompatResources;
import androidx.cardview.widget.CardView;
import androidx.core.content.ContextCompat;

import com.gewu.pm.R;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.other.IntentKey;
import com.gewu.pm.utils.TextStarUtils;
import com.gewu.pm.utils.eventbus.EventBean;
import com.gewu.pm.utils.eventbus.EventCode;

/**
 * 拜访客户  签到打卡流程
 */
public class VisitCustomerActivity extends AppActivity {

    private String type;//界面类型

    /**
     * type
     * 1. 可以签到(在打卡范围)
     * 2. 不可以签到(不在打卡范围)
     * 3. 已签到
     * 4. 任务完成
     * 5. 已签退
     */
    public static void start(Context context, String type) {
        Intent intent = new Intent(context, VisitCustomerActivity.class);
        intent.putExtra(IntentKey.TYPE, type);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.ativity_visit_customer;
    }

    private ImageView dot_check_in;//第1个节点  签到
    private ImageView dot_task;//第2个节点  任务
    private ImageView dot_check_out;//第3个节点  签退
    private View line_check_in;//第1个线
    private View line_task;//第2个线

    private TextView tv_check_in_time;//签到的时间

    private LinearLayout layout_check_name;//签到打卡的姓名和范围布局
    private View tv_check_name;//签到打卡的姓名

    private LinearLayout layout_check_img;//签到打卡的点击按钮
    private CardView card_check_in_circle;//签到打卡的点击按钮背景
    private TextView tv_check_in_scope;//签到打卡的 是否在区域可打卡

    private LinearLayout layout_check_out_img;//签退打卡的布局

    private TextView tv_manager_go_fill;//去填写促销拜访信息

    private TextView tvManager;
    private LinearLayout layoutComplete;

    @Override
    protected void initView(Bundle savedInstanceState) {
        dot_check_in = findViewById(R.id.dot_check_in);
        dot_task = findViewById(R.id.dot_task);
        dot_check_out = findViewById(R.id.dot_check_out);
        line_check_in = findViewById(R.id.line_check_in);
        line_task = findViewById(R.id.line_task);
        layout_check_name = findViewById(R.id.layout_check_name);
        tv_check_name = findViewById(R.id.tv_check_name);
        layout_check_img = findViewById(R.id.layout_check_img);
        card_check_in_circle = findViewById(R.id.card_check_in_circle);
        tv_check_in_scope = findViewById(R.id.tv_check_in_scope);
        tv_check_in_time = findViewById(R.id.tv_check_in_time);
        layout_check_out_img = findViewById(R.id.layout_check_out_img);
        tv_manager_go_fill = findViewById(R.id.tv_manager_go_fill);

        tvManager = findViewById(R.id.tv_manager);
        TextStarUtils.setRequiredText(tvManager,"促销拜访信息填写");//给文字加前面的红色星星
        layoutComplete = findViewById(R.id.layout_complete);

        setOnClickListener(layoutComplete);

    }

    @Override
    protected void initData() {
        type = getString(IntentKey.TYPE);
        updateUI();
    }

    @Override
    protected boolean isRegisteredEventBus() {
        return true;  // 启用 EventBus
    }

    @Override
    protected void receiveEvent(EventBean event) {
        if (event.getCode() == EventCode.VISIT_INFO_SUBMIT) {
            // 接收拜访信息提交事件
            String totalAmount = (String) event.getData();
            if (totalAmount != null) {
                toast("费用合计：" + totalAmount + " 元");
            }
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == layoutComplete){//填写编辑内容
            // 跳转到终端拜访信息填写页面
            TerminalVisitInfoActivity.start(this);
        }
    }

    private void updateUI() {
        // 根据不同状态切换不同的UI显示
        switch (type) {
            case "1"://1 可以签到(在打卡范围)
                hideCheckInComplete();
                layout_check_img.setVisibility(View.VISIBLE);//显示签到打卡的点击按钮
                tv_manager_go_fill.setVisibility(View.VISIBLE);//显示促销拜访的去填写  按说不应该显示的
                tv_check_in_time.setText("签到打卡");
                card_check_in_circle.setCardBackgroundColor(getResources().getColor(R.color.orange_01));//签到打卡的颜色  目前与UI有出入
                Drawable drawable = AppCompatResources.getDrawable(this, R.drawable.icon_position_green);
                tv_check_in_scope.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null);
                tv_check_in_scope.setText("已经入签到范围");

                break;
            case "2"://2. 不可以签到(不在打卡范围)
                hideCheckInComplete();
                layout_check_img.setVisibility(View.VISIBLE);//显示签到打卡的点击按钮
                tv_manager_go_fill.setVisibility(View.VISIBLE);//显示促销拜访的去填写  按说不应该显示的
                tv_check_in_time.setText("签到打卡");
                card_check_in_circle.setCardBackgroundColor(getResources().getColor(R.color.cb6));
                Drawable drawable2 = AppCompatResources.getDrawable(this, R.drawable.icon_red_warn);
                tv_check_in_scope.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable2, null, null, null);

                tv_check_in_scope.setText("当前不在签到范围");

                break;
            case "3"://3.已签到
                hideCheckInComplete();
                tv_manager_go_fill.setVisibility(View.VISIBLE);//点击签到后  是可以去填写拜访信息的

                tv_check_in_time.setText("签到时间  2025年11月3日16:47:59");
                layout_check_name.setVisibility(View.VISIBLE);//签到打卡的姓名和范围布局

                //线和点
                dot_check_in.setBackgroundResource(R.drawable.icon_complete_green);
                line_check_in.setBackgroundColor(ContextCompat.getColor(this, R.color.green_01));

                break;
            case "4"://4. 任务完成
                hideCheckInComplete();
                tv_check_in_time.setText("签到时间  2025年11月3日16:47:59");
                layout_check_name.setVisibility(View.VISIBLE);//显示签到打卡的姓名和范围布局
                layout_check_out_img.setVisibility(View.VISIBLE);//显示签退打卡的按钮
                //线和点
                dot_check_in.setBackgroundResource(R.drawable.icon_complete_green);
                dot_task.setBackgroundResource(R.drawable.icon_complete_green);
                line_check_in.setBackgroundColor(ContextCompat.getColor(this, R.color.green_01));
                line_task.setBackgroundColor(ContextCompat.getColor(this, R.color.green_01));

                break;
            case "5"://5. 已签退
                hideCheckInComplete();
                tv_check_in_time.setText("签到时间  2025年11月3日16:47:59");
                layout_check_name.setVisibility(View.VISIBLE);//签到打卡的姓名和范围布局

                //线和点
                dot_check_in.setBackgroundResource(R.drawable.icon_complete_green);
                dot_task.setBackgroundResource(R.drawable.icon_complete_green);
                dot_check_out.setBackgroundResource(R.drawable.icon_complete_green);
                line_check_in.setBackgroundColor(ContextCompat.getColor(this, R.color.green_01));
                line_task.setBackgroundColor(ContextCompat.getColor(this, R.color.green_01));

                break;

        }
    }

    /**
     * 重置布局，都隐藏
     */
    private void hideCheckInComplete() {
        layout_check_name.setVisibility(View.GONE);
        layout_check_img.setVisibility(View.GONE);
        layout_check_out_img.setVisibility(View.GONE);
        tv_manager_go_fill.setVisibility(View.GONE);
        tv_check_in_time.setText("签到打卡");

        dot_check_in.setBackgroundResource(R.drawable.timeline_dot_grey);
        dot_task.setBackgroundResource(R.drawable.timeline_dot_grey);
        dot_check_out.setBackgroundResource(R.drawable.timeline_dot_grey);

        line_check_in.setBackgroundColor(ContextCompat.getColor(this, R.color.grey_01));
        line_task.setBackgroundColor(ContextCompat.getColor(this, R.color.grey_01));
    }

}
